package com.future.hedongjun.home.adapter;

import android.content.Context;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.future.baselib.utils.ToastUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.home.bean.ClassCoursewareBean;
import com.future.hedongjun.home.utils.OnItemClickListener;
import com.future.hedongjun.mine.bean.FileBean;
import com.future.hedongjun.mine.utils.CallOtherFile;
import com.future.hedongjun.mine.utils.FileUtil;
import com.kingsoft.media.httpcache.KSYProxyService;
import com.kingsoft.media.httpcache.OnCacheStatusListener;
import com.kingsoft.media.httpcache.OnErrorListener;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * 创建日期：2018/11/20 10:52
 * author liu.p.f
 * 类说明： 课件适配器
 */
public class ClassCoursewareAdapter extends RecyclerView.Adapter<ClassCoursewareAdapter.MyViewHolder> implements View.OnClickListener {

    private TextView tvDown;
    private OnItemClickListener mItemClickListener;
    private Context mContext;
    private List<ClassCoursewareBean> mDatas;
    private MyViewHolder viewholder;
    private int mPostion;
    private final KSYProxyService mProxyService;

    public ClassCoursewareAdapter(Context context, List<ClassCoursewareBean> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;

        mProxyService = MyApp.getKSYProxy(mContext);
        mProxyService.registerErrorListener(mOnErrorListener);
        mProxyService.setMaxFilesCount(500);
        mProxyService.setCacheRoot(new File(Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheDoc" + "/" + mDatas.get(0).ClassName+"####"+mDatas.get(0).classLogo));
        mProxyService.startServer();
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.recycler_item_class_courseware, null);
        MyViewHolder holder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        viewholder = (MyViewHolder) holder;
        holder.tvName.setText(mDatas.get(position).courseName);
        holder.itemView.setTag(position);

        for (Object o : mProxyService.getCachedFileList().entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String url = (String) entry.getKey();
            for (int i = 0; i < mDatas.size(); i++) {
                if (mDatas.get(i).courseUrl.equals(url)) {
                    mDatas.get(i).setDownNum(100);
                }
            }
        }

        if (mDatas.get(position).getDownNum() > 0 && mDatas.get(position).getDownNum() < 100) {
            holder.tvDown.setText(mDatas.get(position).getDownNum() + "%");
        } else if (mDatas.get(position).getDownNum() == 100) {
            holder.tvDown.setText("打开");
            holder.tvDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CallOtherFile callOtherFile = new CallOtherFile();
                    callOtherFile.openFile(mContext, mProxyService.getCacheFile(mDatas.get(position).courseUrl));
                }
            });
        } else {
            holder.tvDown.setText("下载");
            // 绑定进度条
            holder.tvDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPostion = position;
                    mProxyService.startPreDownload(mDatas.get(position).courseUrl);
                    mProxyService.registerCacheStatusListener(mCacheStatusListener, mDatas.get(position).courseUrl);
                }
            });
        }

    }

    private OnErrorListener mOnErrorListener = new OnErrorListener() {
        @Override
        public void OnError(int errCode) {
            Toast.makeText(mContext, "HttpCache meet an error:" + errCode, Toast.LENGTH_SHORT).show();
        }
    };

    private OnCacheStatusListener mCacheStatusListener = new OnCacheStatusListener() {
        @Override
        public void OnCacheStatus(String url, long sourceLength, int percentsAvailable) {
            updateItem(mPostion, percentsAvailable);

        }
    };

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(v);
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvDown;
        ImageView mDownloadView;
        // CirclePgBar downloading_CirclePgBar;

        public MyViewHolder(View view) {
            super(view);
            tvName = (TextView) itemView.findViewById(R.id.tv_coursewareName);
            tvDown = (TextView) itemView.findViewById(R.id.tv_downCourseware);
            mDownloadView = (ImageView) itemView.findViewById(R.id.iv_coursewareIcon);
            //downloading_CirclePgBar = (CirclePgBar) itemView.findViewById(R.id.downloading_CirclePgBar);
        }
    }

    /**
     * 对外接口
     *
     * @param itemClickListener
     */
    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }


    private void updateItem(int position, int value) {
        if (position >= 0 && position < mDatas.size()) {
            mDatas.get(position).setDownNum(value);
            notifyItemChanged(position);
        }
    }
}


